package com.jsgt.greekfire.ui.home

import android.content.Context
import android.content.Intent
import android.net.http.SslError
import android.webkit.*
import com.fhx.core.base.BaseActivity
import com.jsgt.greekfire.R
import kotlinx.android.synthetic.main.activity_web.*

/**
 * author fhx
 * create time 2020/5/4 17:31
 **/
class WebActivity : BaseActivity() {

    companion object {
        const val WEB_URL = "url"
        const val TITLE = "title"
        fun start(context: Context, url: String, title: String) {
            val intent = Intent(context, WebActivity::class.java)
            intent.putExtra(WEB_URL, url)
            intent.putExtra(TITLE, title)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_web

    override fun main() {
        title = intent.getStringExtra(TITLE).toString()
        val webSettings = webView.settings
        webSettings.domStorageEnabled = true//主要是这句
        webSettings.javaScriptEnabled = true;//启用js
        webSettings.blockNetworkImage = false//解决图片不显示
        webSettings.savePassword = false
        webSettings.javaScriptCanOpenWindowsAutomatically = true
        webSettings.loadsImagesAutomatically = true
        webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
        webView.webChromeClient = WebChromeClient()
        webView.loadUrl(intent.getStringExtra(WEB_URL).toString())
        webView.webViewClient = object : WebViewClient() {
            override fun onReceivedSslError(
                view: WebView?,
                handler: SslErrorHandler?,
                error: SslError?
            ) {
                handler?.proceed()
            }
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                view?.loadUrl(url)
                return true
            }
        }
    }
}